Skip to content

Conversation

KushalMeghani1644
Copy link

@KushalMeghani1644 KushalMeghani1644 commented Oct 6, 2025

This PR adds an optional feature to skip .data initialization during startup.

Introduces:

  • the skip-data-init feature that disables the .data copy loop in the reset handler, for boot flows that already initialize RAM (e.g. RP2040 boot2 that copies to RAM and unmaps flash).

  • Adds a compile-time guard preventing skip-data-init from being used with zero-init-ram.

  • Includes a minimal example at skip-data-init.rs.

  • Default behavior is unchanged; .data is still copied when the feature is not enabled.

This PR tries to address issue #609

@jonathanpallant
Copy link
Contributor

Stubbing out the copy loop seems fine but what is the purpose of the example? It has no observable output and no mechanism to run it with the kind of bootloader that it must be used with to avoid UB. Was it LLM generated?

@KushalMeghani1644
Copy link
Author

Hey @jonathanpallant the purpose of that example is to demonstrate how a Cortex-M program behaves when the usual .data section initialization is skipped by cortex-m-rt. This is useful because normally the cortex-m-rt copies .data section from flash to RAM. But for some scenarios when using the RP2040 boot2 bootloader as stated in issue #609 this copy has been already done by the bootloader itself, and the cortex-m-rt copies again, basically it reads from unmapped memory causing undefined behaviour.

Either way my PR isn't LLM generated, through that example I was indeed trying to show something reasonable :)

Thanks!

@jonathanpallant
Copy link
Contributor

Ok so how do I run the example and what will I observe when the feature is set?


use cortex_m_rt::entry;

static mut COUNTER: u32 = 42;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be put into local scope or replaced by an atomic to avoid unsafe code?

unsafe {
COUNTER += 1;
}

Copy link

@robamu robamu Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you just print the counter using semihosting printout?

Also, what about panicking if the output is not the expected value?

@robamu
Copy link

robamu commented Oct 7, 2025

I think you might be able to just use a non-zero constant here to show that the data section was initialized? No need to use mutable statics then..

EDIT: the constant might be optimized away..

@Dirbaio
Copy link
Member

Dirbaio commented Oct 7, 2025

@KushalMeghani1644 have you actually tried if this solves the issue #609 is reporting, in actual RP2040 hardware, with the copy-to-ram boot2?

@jonathanpallant
Copy link
Contributor

I'm closing this PR on the grounds it's taking far more effort to review than the change is worth.

I'm happy to look at this again when we have a working example (in another repo) with clear steps to reproduce the issue and clear evidence that the PR resolves the issue.

@KushalMeghani1644 KushalMeghani1644 deleted the add_skip_data_copy branch October 7, 2025 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants